package com.ipc.app.web.filter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;

import com.ipc.app.constants.SessionConstants;

/**
 * @author haijie.zhang
 * 
 */
public class AuthenticationFilter extends HttpServlet implements Filter {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7650923910700498994L;

	private String loginURI;

	private List<String> ignoreURIs;

	private List<String> ignoreResources;

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain filterChain) throws IOException, ServletException {
		HttpServletRequest httpRequest = (HttpServletRequest) request;
		HttpServletResponse httpResponse = (HttpServletResponse) response;

		HttpSession session = httpRequest.getSession(false);

		String requestURI = httpRequest.getRequestURI();

		if (requestURI.contains("?")) {
			requestURI = requestURI.split("?")[0];
		}

		// 过滤IgnoreURI
		if (ignoreURIs.contains(requestURI)) {
			filterChain.doFilter(request, response);
			return;
		}

		// 过滤资源
		String resource = StringUtils.substringAfterLast(requestURI, ".");
		if (ignoreResources.contains(resource)) {
			filterChain.doFilter(request, response);
			return;
		}

		if (null != session) {
			String loginUser = (String) session
					.getAttribute(SessionConstants.LOGIN_USER_ID);
			if (StringUtils.isNotBlank(loginUser)) {
				filterChain.doFilter(request, response);
				return;
			}
		}

		StringBuffer targetURI = new StringBuffer("?targetURI=");
		targetURI.append(requestURI);
		httpResponse.sendRedirect(httpRequest.getContextPath() + loginURI
				+ targetURI.toString());
	}

	public void init(FilterConfig filterConfig) throws ServletException {
		loginURI = filterConfig.getInitParameter("loginURI");
		this.initIngoreURIS(filterConfig);
		this.initIngoreResources(filterConfig);

	}

	private void initIngoreResources(FilterConfig filterConfig) {
		String ignoreResource = filterConfig
				.getInitParameter("ignoreResources");
		String[] ignorResourcesSpilit = ignoreResource.split(",");

		ignoreResources = new ArrayList<String>();

		for (String resource : ignorResourcesSpilit) {
			ignoreResources.add(resource);
		}
	}

	/**
	 * @param filterConfig
	 */
	private void initIngoreURIS(FilterConfig filterConfig) {
		String originalURIS = filterConfig.getInitParameter("ignoreURI");
		String[] originalURISpilit = originalURIS.split(",");

		ignoreURIs = new ArrayList<String>();

		for (String uri : originalURISpilit) {
			uri = StringUtils.trim(uri);
			ignoreURIs.add(uri);
		}

	}

}
